package com.example.androidlearning.sy13;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.androidlearning.R;

public class BroadcastActivity extends AppCompatActivity implements View.OnClickListener {

    TextView song, musician;
    ImageButton play, stop;
    ActivityReceiver activityReceiver;
    SeekBar seekBar;

    public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";
    public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";

    //定义播放状态， 0x11没有播放；0x12正在播放；0x13暂停
    int status = 0x11;
    String[] songs;
    String[] musicians;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy1301_main);

        init();
    }

    private void init(){
        play = findViewById(R.id.sy13_play);
        stop = findViewById(R.id.sy13_stop);
        song = findViewById(R.id.sy13_song);
        musician = findViewById(R.id.sy13_musician);
        seekBar = findViewById(R.id.sy13_seekbar);

        songs = new String[]{"bocaixiaomiououtou"};
        musicians = new String[]{"群星"};

        play.setOnClickListener(this);
        stop.setOnClickListener(this);


        activityReceiver = new ActivityReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(UPDATE_ACTION);
        registerReceiver(activityReceiver, filter);
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
    }

    public class ActivityReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            int update = intent.getIntExtra("update", -1);
            int current = intent.getIntExtra("current", -1);
            if(current >= 0){
                song.setText(songs[current]);
                musician.setText(musicians[current]);
            }
            switch (update){
                case 0x11:
                    play.setBackgroundResource(R.drawable.zanting);
                    status = 0x11;
                    break;
                case 0x12:
                    play.setBackgroundResource(R.drawable.bofang);
                    status = 0x12;
                    break;
                case 0x13:
                    play.setBackgroundResource(R.drawable.zanting);
                    status = 0x13;
                    break;
            }
        }
    }

    private Context getContext(){
        return this;
    }


    @Override
    public void onClick(View v) {

        Intent intent = new Intent(CTL_ACTION);
        switch (v.getId()){
            case R.id.sy13_play:
                intent.putExtra("control", 1);
                break;
            case R.id.sy13_stop:
                intent.putExtra("control", 2);
                break;
        }
        sendBroadcast(intent);
    }
}
